package cc.rengu.redp.bizimpl.igas.service.impl;

import cc.rengu.redp.bizimpl.igas.dao.PDstTxnAuthCfgMapper;
import cc.rengu.redp.bizimpl.igas.domain.PDstTxnAuthCfg;
import cc.rengu.redp.bizimpl.igas.service.PDstTxnAuthCfgService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.Common;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.common.exception.RedpException;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 刘玉亮.LL
 * @since 2021-06-09
 */
@DS("igas")
@Service
public class PDstTxnAuthCfgServiceImpl extends ServiceImpl<PDstTxnAuthCfgMapper, PDstTxnAuthCfg> implements PDstTxnAuthCfgService {
    private final static Logger logger = LoggerFactory.getLogger(PDstTxnAuthCfgService.class);
    @Autowired
    private IUpmpSysParamService upmpSysParamService;
    @Autowired
    private PDstTxnAuthCfgService pDstTxnAuthCfgService;

    @Override
    public List<PDstTxnAuthCfg> findPDstTxnAuthCfgListByDstChannelId(String instId, String msgDstId, String channelTxn){
        LambdaQueryWrapper<PDstTxnAuthCfg> wrapper = new LambdaQueryWrapper<>();
        if (Common.isBlank(instId)) {
            UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
            if (Common.isNotBlank(user) && !Common.isBlank(user.getInstId())) {
                instId = user.getInstId();
                wrapper.eq(PDstTxnAuthCfg::getInstId, instId);
            }
        } else {
            wrapper.eq(PDstTxnAuthCfg::getInstId, instId);
        }
        if (!StringUtils.isBlank(msgDstId)) {
            wrapper.eq(PDstTxnAuthCfg::getMsgDstId, msgDstId);
        }
        if (!StringUtils.isBlank(channelTxn)) {
            wrapper.eq(PDstTxnAuthCfg::getChannelTxn, channelTxn);
        }

        return this.list(wrapper);

    }

    @Override
    public PDstTxnAuthCfg findOne(String instId, String msgDstId, String channelTxn) throws RedpException {

        QueryWrapper<PDstTxnAuthCfg> queryWrapper=new QueryWrapper<>();
        if(Common.isNotBlank(instId)){
            queryWrapper.eq("inst_id",instId);
        }else{
            throw new RedpException("查询支付通道配置信息唯一索引列[instId]条件为空");
        }
        if(!Common.isBlank(msgDstId)){
            queryWrapper.eq("msg_dst_id",msgDstId);
        }else{
            throw new RedpException("查询支付通道配置信息唯一索引列[msgDstId]条件为空");
        }
        if(!Common.isBlank(channelTxn)){
            queryWrapper.eq("channel_txn",channelTxn);
        }else{
            throw new RedpException("查询支付通道配置信息唯一索引列[channelTxn]条件为空");
        }
        PDstTxnAuthCfg one = pDstTxnAuthCfgService.getOne(queryWrapper);
        System.out.println(one);
        return one;
    }
    @Override
    public void updatePDstTxnAuthCfg(PDstTxnAuthCfg pDstTxnAuthCfg) throws  RedpException {
        QueryWrapper<PDstTxnAuthCfg> wrapper=new QueryWrapper<>();

        if(Common.isNotBlank(pDstTxnAuthCfg.getInstId())){
            wrapper.eq("INST_ID",pDstTxnAuthCfg.getInstId());
        }else{
            throw new RedpException("[instId]条件为空");
        }
        if(!Common.isBlank(pDstTxnAuthCfg.getMsgDstId())){
            wrapper.eq("MSG_DST_ID",pDstTxnAuthCfg.getMsgDstId());
        }else{
            throw new RedpException("[msgDstId]条件为空");
        }
        if(!Common.isBlank(pDstTxnAuthCfg.getChannelTxn())){
            wrapper.eq("CHANNEL_TXN",pDstTxnAuthCfg.getChannelTxn());
        }else{
            throw new RedpException("[channelTxn]条件为空");
        }

        pDstTxnAuthCfgService.update(pDstTxnAuthCfg,wrapper);
    }
}
